package com.sixth.exceptionhandler;

import com.sixth.annotation.ParameterValidation;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.exception.CustomValidationException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 处理进行参数校验的方法抛出的异常，
 * 返回格式为
 * errno：402，errmsg："参数值不对"
 * @author qys
 * @Description
 * @date 2022/9/9 14:25
 */
@RestControllerAdvice(annotations = {ParameterValidation.class})
public class ValidationExceptionControllerAdvice {

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public BaseRespVo test(HttpMessageNotReadableException httpMessageNotReadableException) {
        return BaseRespVo.invalidNoAndMsg("参数值不对", 402);
    }


    @ExceptionHandler({SQLIntegrityConstraintViolationException.class, MethodArgumentNotValidException.class, CustomValidationException.class})
    public BaseRespVo validation() {
        return BaseRespVo.invalidNoAndMsg("参数值不对", 402);
    }
}
